package com.sqx.modules.wxMsg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jfinal.weixin.sdk.msg.in.InLocationMsg;
import com.jfinal.weixin.sdk.msg.in.InTextMsg;
import com.jfinal.weixin.sdk.msg.in.event.InFollowEvent;
import com.jfinal.weixin.sdk.msg.in.event.InLocationEvent;
import com.jfinal.weixin.sdk.msg.in.event.InMenuEvent;
import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.service.CommonInfoService;
import net.dreamlu.weixin.annotation.WxMsgController;
import net.dreamlu.weixin.spring.DreamMsgControllerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
;

/**
 * 微信公众号消息
 */
@WxMsgController("/weixin/wx")
public class WeiXinMsgController extends DreamMsgControllerAdapter {
    @Autowired
    private UserService userService;
    @Autowired
    private CommonInfoService commonInfoService;

    /**
     * 处理接收到的关注/取消关注事件
     * @param inFollowEvent 处理接收到的关注/取消关注事件
     */
    @Override
    protected void processInFollowEvent(InFollowEvent inFollowEvent) {
        //未注册提示
        String unregisteredTips = commonInfoService.findOne(345).getValue();
        String successTips = commonInfoService.findOne(346).getValue();
        OutTextMsg outMsg = new OutTextMsg(inFollowEvent);
        String event = inFollowEvent.getEvent();
        if ("subscribe".equals(event)) {
            UserEntity userEntity = userService.getOne(new QueryWrapper<UserEntity>().eq("account_open_id", inFollowEvent.getFromUserName()));
            if (userEntity == null) {
                outMsg.setContent(unregisteredTips);
                render(outMsg);
            } else {
                userEntity.setAccountOpenId(inFollowEvent.getFromUserName());
                userService.updateById(userEntity);
                outMsg.setContent(userEntity.getPhone() + successTips);
                render(outMsg);
            }
        }
    }

    /**
     * 处理接收到的文本消息
     * @param inTextMsg 处理接收到的文本消息
     */
    @Override
    protected void processInTextMsg(InTextMsg inTextMsg) {
        OutTextMsg outMsg = new OutTextMsg(inTextMsg);
        render(outMsg);
    }

    /**
     * 处理接收到的自定义菜单事件
     * @param inMenuEvent 处理接收到的自定义菜单事件
     */
    @Override
    protected void processInMenuEvent(InMenuEvent inMenuEvent) {
        OutTextMsg outMsg = new OutTextMsg(inMenuEvent);
        outMsg.setContent("菜单消息");
        render(outMsg);
    }

    /**
     * 处理接收到的地址位置消息(用户端主动发送当前位置信息)
     * @param inLocationMsg 处理接收到的地址位置消息
     */
    @Override
    protected void processInLocationMsg(InLocationMsg inLocationMsg) {


        super.processInLocationMsg(inLocationMsg);
    }


    /**
     * 处理接收到的上报地理位置事件(用户端自动推送当前位置信息)
     * @param inLocationEvent 处理接收到的上报地理位置事件
     */
    @Override
    protected void processInLocationEvent(InLocationEvent inLocationEvent) {

    }
}
